package hello

import (
	"database/sql"
	"strings"

	_ "github.com/go-sql-driver/mysql"
)

// const (
// 	DEBUG   = "Debug"
// 	WARNING = "Warnning"
// 	ERROR   = "Error"
// )

const (
	userName = "jiayu"
	password = "jiayu123"
	ip       = ""
	port     = "3306"
	dbName   = "test"
)

type Storage struct {
	db *sql.DB
}

func NewMysqlInit() (*Storage, error) {
	path := strings.Join([]string{userName, ":", password, "@tcp(", ip, ":", port, ")/", dbName, "?charset=utf8"}, "")
	db, err := sql.Open("mysql", path)
	if err != nil {
		Logger(ERROR, "db connect err...... ")
		return nil, err
	}
	defer db.Close()

	if connect_err := db.Ping(); connect_err != nil {
		Logger(ERROR, "连接失败.......")
		return nil, err
	}
	Logger(DEBUG, "db connect sucess! \n")
	return nil, err
}

// 查询方法
func (storage *Storage) Query(queryStr string) string {
	var id, name, path string
	rows, err := storage.db.Query(queryStr)
	if err != nil {
		errStr := "查询出错,Sql语句"
		return errStr
	}
	defer rows.Close()
	rows.Scan(&id, &name, &path)
	return path
}
